package com.durian.ui.dialog

import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding
import com.durian.base.R
import com.durian.base.frame.activity.ActivityFrame
import com.durian.base.frame.fragment.FragmentFrame

/**
 * 说明：SuperNiceDialog
 * <p/>
 * 作者：Fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2020/08/17 20:25
 * <p/>
 * 版本：version 1.0
 */
@Deprecated("")
class SuperNiceDialog<VB : ViewBinding>(val clazz: Class<VB>) : BaseNiceDialog() {

    private var convertListener: ((VB, SuperNiceDialog<VB>?) -> Unit)? = null
    private var onDismissListener: DialogInterface.OnDismissListener? = null
    private lateinit var viewBinding : VB

    init {
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.base_ui_nice_dialog)
    }


    override fun intLayoutId(): Int {
        return layoutId
    }

    override fun bindRootView(inflater: LayoutInflater?, viewGroup: ViewGroup?): View {
        return viewBinding.root
    }

    override fun convertView(holder: ViewHolder?, dialog: BaseNiceDialog?) {
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onDismissListener?.onDismiss(dialog)
    }

    fun setOnDismissListener(listener: DialogInterface.OnDismissListener?): SuperNiceDialog<VB>? {
        this.onDismissListener = listener
        return this
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        viewBinding = createViewBinding(inflater,container)
        convertListener?.invoke(viewBinding,this)
        return viewBinding.root
    }

    private fun createViewBinding(inflater: LayoutInflater,container: ViewGroup?):VB{
        val method = clazz.getMethod("inflate",LayoutInflater::class.java,ViewGroup::class.java,Boolean::class.java)
        return method.invoke(null,inflater,container,false) as VB
    }

    fun setConvertListener(block: ((VB, BaseNiceDialog?) -> Unit)? = null): SuperNiceDialog<VB>? {
        this.convertListener = block
        return this
    }

    fun show(fragment: FragmentFrame, func: SuperNiceDialog<VB>.() -> Unit) {
        func.invoke(this)
        show(fragment.childFragmentManager)
    }

    fun show(activity: ActivityFrame, func: SuperNiceDialog<VB>.() -> Unit) {
        func.invoke(this)
        show(activity.supportFragmentManager)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    /**
     * 保存接口
     *
     * @param outState
     */
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }
}